import { login, getprofile } from '@/api/user'
// import { login } from '@/api/user'
//
import { resetRouter } from '@/router'

const state = {
  token: localStorage.getItem('token') || '',
  profile: null
}
const mutations = {
  gettoken(state, data) {
    // console.log(10, state, data)
    state.token = data
    localStorage.setItem('token', data)
  },
  getfile(state, data) {
    state.profile = data
  },
  removetoken(state) {
    state.token = ''
    localStorage.removeItem('token')
  },
  removeprofile(state) {
    state.profile = ''
  }
}
const actions = {
  async login(store, data) {
    const res = await login(data)
    console.log(28, 'user', res)
    store.commit('gettoken', res.jwt)
    console.log('会继续执行')
    // 主动介入token失效步骤一 登录后记录时间
    localStorage.setItem('logintime', Date.now())
  },
  async getfile(store) {
    const res = await getprofile()
    // const datalist = await getprofiledata(res.userId)
    console.log(26, res)
    store.commit('getfile', { ...res })
  },
  logout(store) {
    store.commit('removetoken')
    store.commit('removeprofile')

    resetRouter()
  }

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

